import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

class XinDongInputTextSendSheet {
  XinDongInputTextSendSheet.show(
      {required String? text,
      String placeholder = 'Say something…',
      String buttonName = "发送",
      TextEditingController? editingController,
      required Function(String text, int cursor) textChange,
      required Function(String text) onTapSend}) {
    showInputSheetDialog(
      sheet: XinDongInputTextSendBar(
        text: text ??= '',
        placeholder: placeholder,
        editingController: editingController,
        buttonName: buttonName,
        textChange: textChange,
        onTapSend: onTapSend,
      ),
    );
  }
}

class XinDongInputTextSendBar extends StatefulWidget {
  final String text;
  final String placeholder;
  final String buttonName;
  final TextEditingController? editingController; // 编辑控制器，选填。为空时会使用内部的控制器
  final Function(String text, int cursor) textChange; // 文字和光标在输入框文字位置
  final Function(String text) onTapSend;

  const XinDongInputTextSendBar(
      {Key? key,
      required this.text,
      required this.placeholder,
      required this.buttonName,
      this.editingController,
      required this.textChange,
      required this.onTapSend})
      : super(key: key);

  @override
  State<XinDongInputTextSendBar> createState() => _XinDongInputTextSendBarState();
}

class _XinDongInputTextSendBarState extends State<XinDongInputTextSendBar> {
  bool enableSend = false;
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    if (widget.editingController == null) {
      _controller = TextEditingController(text: widget.text);
    } else {
      _controller = widget.editingController!;
    }
    // 光标保持在最后
    // _controller.selection = TextSelection.fromPosition(TextPosition(offset: _controller.text.length));
    enableSend = _controller.text.isNotEmpty;
  }

  @override
  void dispose() {
    if (widget.editingController == null) {
      _controller.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(12, 10, 12, 10),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(topLeft: Radius.circular(5), topRight: Radius.circular(5)),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(width: MediaQuery.of(context).padding.left),
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                color: DZColors.hexStringColor("F7F8FA"),
                borderRadius: BorderRadius.circular(16),
              ),
              child: TextField(
                controller: _controller,
                onChanged: (text) {
                  widget.textChange(text, _controller.selection.baseOffset);
                  setState(() {
                    enableSend = _controller.text.isNotEmpty;
                  });
                },
                autofocus: true,
                minLines: 1,
                maxLines: 3,
                style: titleTextStyle,
                textInputAction: TextInputAction.unspecified,
                keyboardType: TextInputType.multiline,
                decoration: InputDecoration(
                  hintText: widget.placeholder,
                  hintStyle: placeholderTextStyle,
                  isDense: true,
                  contentPadding: const EdgeInsets.only(left: 10, top: 8, bottom: 8, right: 10),
                  border: const OutlineInputBorder(
                    gapPadding: 0,
                    borderSide: BorderSide(width: 0, style: BorderStyle.none),
                  ),
                ),
              ),
            ),
          ),
          const SizedBox(width: 8),
          SizedBox(
            width: 64,
            child: MaterialButton(
              onPressed: enableSend
                  ? () {
                      widget.onTapSend(_controller.text);
                      Navigator.pop(context);
                    }
                  : null,
              height: 36.0,
              elevation: 0,
              textColor: Colors.white,
              disabledTextColor: DZColors.titleColor,
              color: DZColors.mainColor,
              disabledColor: DZColors.disabledColor,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(32.0)),
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              child: Text(
                widget.buttonName,
                style: const TextStyle(fontSize: 15, height: 1.2),
              ),
            ),
          ),
          SizedBox(width: MediaQuery.of(context).padding.right),
        ],
      ),
    );
  }
}
